package com.atguigu.gulimall.search.Thread;

import java.util.concurrent.*;

public class ThreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        System.out.println("main......start");
//        Thread01 thread = new Thread01();
//        Runable01 runable01 = new Runable01();
//        FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
//        new Thread(futureTask).start();
//        Integer integer = futureTask.get();
//        new Thread(runable01).start();
//        System.out.println("main.......end");
//        service.execute(new Runable01());
//        System.out.println("main.......end"+integer);
        System.out.println("main......start");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(()->{
            System.out.println("当前线程："+Thread.currentThread().getId());
            int i = 10/0;
            System.out.println("运行结果:"+i);
            return i;
        },executor).whenComplete((result,exception)->{
            System.out.println("异步任务成功完成.....结果是"+result+"异常是..."+exception);
        }).exceptionally(throwable -> {
            return 10;
        });
        Integer integer = future.get();
        System.out.println("main.......end......." +integer);
    }
    public static class Thread01 extends Thread{
        @Override
        public void run(){
            System.out.println("当前线程："+Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果:"+i);
        }
    }

    public static class Runable01 implements Runnable{
        @Override
        public void run(){
            System.out.println("当前线程："+Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果:"+i);
        }
    }

    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception{
            System.out.println("当前线程："+Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果:"+i);
            return i;
        }
    }
}
